package com.itheima.junitdemo;

import java.lang.reflect.Method;

public class AnnotationTest {

    public static void main(String[] args) {
        // 1.获取类对象,并使用反射构造对象
        Class clazz = AnnotationTest.class;
        AnnotationTest annotationTest = null;
        try {
            annotationTest = (AnnotationTest) clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 2. 获取方法
        Method[] methods = clazz.getMethods();
        // 3.遍历方法
        for (Method method : methods) {
            // 4.判断方法上是否有MyTest2注解
            if (method.isAnnotationPresent(MyJunit.class)) {
                // 5.执行方法
                try {
                    for (int i = 0; i < method.getAnnotation(MyJunit.class).count(); i++){
                        System.out.println("执行了第" + (i+1) + "次");
                        method.invoke(annotationTest);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @MyJunit
    public void test1(){
        System.out.println("test1");
    }
    public void test2(){
        System.out.println("test2");
    }
    @MyJunit(count = 5)
    public void test3(){
        System.out.println("test3");
    }

    public void test4(){
        System.out.println("test4");
    }
}
